var CostDetailedTable = {
    id: "CostDetailedTable",
    seItem: null,
    table: null,
    layerIndex: -1
};
//主页搜索
$("#doctorTcSearchAlls", parent.document).click(function () {
    costDetail()
});
//重置
CostDetailedTable.resetSearch = function () {
    $("#startTime").val('');
    $("#endTime").val('');
    $('#fieldTc').val('');
    $('#doctor').val('');
    $('#customerStatus').val('');
    $("#doctorTcInputPublics", parent.document).val('');
    costDetail()
};
// 新 市场预约表单查询提交参数
CostDetailedTable.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#doctorTcInputPublics", parent.document).val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['customerStatus'] = $("#customerStatus").val();
    queryData['fieldTc'] = $("#fieldTc").val();
    queryData['doctor'] = $("#doctor").val();
    return queryData;
};
// 把对象拼接成URL的键值对参数 param1=value1&param2=value2
var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {
    return key + "=" + param[key];
};
//导出
CostDetailedTable.exportCostDetailedTable = function () {
    var param = parseParam(CostDetailedTable.formParams());
    location.href = Feng.ctxPath + '/costDetails/exportCostDetailedTable?' + param;
};

var AccountsTime = $("#sttleAccountsTime").val();
CostDetailedTable.naturalDaySearch = function () {
    $("#naturalDaySearch").val('1');
    if ($("#timeValue").val() == "1") {
        $("#startTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(), 0) + " " + "23:59:59")
    } else if ($("#timeValue").val() == "2") {
        $("#startTime").val(setDateTime(new Date(), 1) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(), 1) + " " + "23:59:59")
    } else if ($("#timeValue").val() == "3") {
        var date = new Date();
        var currentWeek = date.getDay();
        var stepSunDay = -currentWeek + 1;
        if (currentWeek == 0) {
            stepSunDay = -7;
        }
        var stepMonday = 7 - currentWeek;
        var time = date.getTime();
        var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
        var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
        $("#startTime").val(setDateTime(monday, 0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(sunday, 0) + " " + "23:59:59")
    }
    if ($("#timeValue").val() == "4") {
        $("#startTime").val(firstMonthTime(new Date(), 0) + " " + "00:00:00");
        $("#endTime").val(setMonthTime(new Date(), 0) + " " + "23:59:59")
    }
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    costDetail()
};

CostDetailedTable.settleAccounts = function () {
    $("#naturalDaySearch").val('2');
    if ($("#timeValue").val() == "1") {
        $("#startTime").val(setDateTime(new Date(), 1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(), 0) + " " + AccountsTime)
    } else if ($("#timeValue").val() == "2") {
        $("#startTime").val(setDateTime(new Date(), 2) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(), 1) + " " + AccountsTime)
    } else if ($("#timeValue").val() == "3") {
        var date = new Date();
        var currentWeek = date.getDay();
        var stepSunDay = -currentWeek + 1;
        if (currentWeek == 0) {
            stepSunDay = -7;
        }
        var stepMonday = 7 - currentWeek;
        var time = date.getTime();
        var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
        var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
        $("#startTime").val(setDateTime(monday, 1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(sunday, 0) + " " + AccountsTime)
    }
    if ($("#timeValue").val() == "4") {
        $("#startTime").val(setMonthTime(new Date(), 1) + " " + AccountsTime);
        $("#endTime").val(setMonthTime(new Date(), 0) + " " + AccountsTime)
    }
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    costDetail()
};

CostDetailedTable.today = function () {//今日
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(new Date(), 0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(), 0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setDateTime(new Date(), 1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(), 0) + " " + AccountsTime);
    }
    $("#timeValue").val('1');
    costDetail()
};

CostDetailedTable.yesterday = function () {//昨日
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(new Date(), 1) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(), 1) + " " + "23:59:59");
    } else {
        $("#startTime").val(setDateTime(new Date(), 2) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(), 1) + " " + AccountsTime);
    }
    $("#timeValue").val('2');
    costDetail()
};

CostDetailedTable.thisWeek = function () {//本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(monday, 0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(sunday, 0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setDateTime(monday, 1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(sunday, 0) + " " + AccountsTime);
    }
    $("#timeValue").val('3');
    costDetail();
};

CostDetailedTable.thisMonth = function () {//本月
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(firstMonthTime(new Date(), 0) + " " + "00:00:00");
        $("#endTime").val(setMonthTime(new Date(), 0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setMonthTime(new Date(), 1) + " " + AccountsTime);
        $("#endTime").val(setMonthTime(new Date(), 0) + " " + AccountsTime);
    }
    $("#timeValue").val('4');
    costDetail();
};

function intervalTime(startTime, endTime) {
    var date1 = new Date(startTime);  //开始时间
    var date2 = new Date(endTime);    //结束时间
    var date3 = date2.getTime() - date1.getTime();  //时间差的毫秒数
    //计算出相差天数
    var days = Math.floor(date3 / (24 * 3600 * 1000));
    //计算出小时数
    var leave1 = date3 % (24 * 3600 * 1000);    //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000));
    //计算相差分钟数
    var leave2 = leave1 % (3600 * 1000);        //计算小时数后剩余的毫秒数
    var minutes = Math.floor(leave2 / (60 * 1000));
    //计算相差秒数
    var leave3 = leave2 % (60 * 1000);      //计算分钟数后剩余的毫秒数
    var seconds = Math.round(leave3 / 1000);
    return days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒";
}

function costDetail() {
    var reach = $("#doctorTcInputPublics", parent.document).val().trim();
    var abbreviation = $("#abbreviation", parent.document).val();
    var areaAbbreviation = $("#areaAbbreviation", parent.document).val();
    var startTime = $('#startTime').val();
    var endTime = $('#endTime').val();
    var fieldTc = $('#fieldTc').val();
    var department = $('#department').val();
    var firstLevelProject = $('#firstLevelProject').val();
    var customerStatus = $('#customerStatus').val();
    var doctor = $('#doctor').val();
    $.ajax({
        type: "post",
        url: Feng.ctxPath + "/costDetails/getCostDetailsReport",
        dataType: "json",
        data: {
            'reach': reach,
            'startTime': startTime,
            'endTime': endTime,
            'fieldTc': fieldTc,
            'doctor': doctor,
            'department': department,
            'firstLevelProject': firstLevelProject,
            'customerStatus': customerStatus,
            'abbreviation': abbreviation,
            'areaAbbreviation': areaAbbreviation
        },
        success: function (data) {
            var totalCost = 0; //总费用
            var priceOriginal = 0; //原价
            var priceOriginalcw = 0; //财务应收
            var Arrears = 0; //欠费
            var totalReceipt = 0; //现金流
            var totalReceiptEquation = 0; //实到金额
            for (var e = 0; e < data.length; e++) {
                totalCost += parseFloat(data[e].总费用);
                priceOriginal += parseFloat(data[e].原价);
                priceOriginalcw += parseFloat(data[e].财务应收);
                Arrears += parseFloat(data[e].欠费);
                totalReceipt += parseFloat(data[e].现金流);
                totalReceiptEquation += parseFloat(data[e].实到金额);
            }
            var arr = [];
            arr.push({radio: true});
            arr.push({"title": '结账时间', "field": '结账时间', switchable: true, sortable: true, align: 'center', width: 170});
            arr.push({"title": '初诊时间', "field": '初诊时间', switchable: true, sortable: true, align: 'center', width: 170});
            arr.push({
                "title": '市场建档时间', "field": '市场建档时间', switchable: true, sortable: true, align: 'center', width: 170,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '时差', "field": '时差', switchable: true, sortable: true, align: 'center', width: 190,
                formatter: function (value, row, index) {
                    if (row.市场建档时间 == 'null' || row.市场建档时间 == '' || row.市场建档时间 == null) {
                        return '';
                    } else {
                        if ("NaN天 NaN小时 NaN 分钟NaN 秒" == intervalTime(row.市场建档时间, row.初诊时间)) {
                            return "";
                        } else {
                            return intervalTime(row.市场建档时间, row.初诊时间);
                        }
                    }
                }
            });
            arr.push({"title": '姓名', "field": '姓名', switchable: true, sortable: true, align: 'center', width: 110});
            arr.push({"title": '性别', "field": '性别', switchable: true, sortable: true, align: 'center', width: 80});
            arr.push({"title": '年龄', "field": '年龄', switchable: true, sortable: true, align: 'center', width: 80});
            arr.push({"title": '病历号', "field": '病历号', switchable: true, sortable: true, align: 'center', width: 145});
            arr.push({"title": '电话', "field": '电话', switchable: true, sortable: true, align: 'center', width: 140});
            arr.push({
                "title": '牙位', "field": '牙位', switchable: true, sortable: true, align: 'center', width: 100,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>';
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            });
            arr.push({"title": '科室', "field": '科室', switchable: true, sortable: true, align: 'center', width: 100});
            arr.push({
                "title": '类别', "field": '类别', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>';
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            });
            arr.push({
                "title": '项目', "field": '项目', switchable: true, sortable: true, align: 'center', width: 180,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>';
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            });
            arr.push({
                "title": '原价', "field": '原价', switchable: true, sortable: true, align: 'center', width: 110,
                formatter: function (value, row, index) {
                    if (index == data.length - 1) {
                        return priceOriginal.toFixed(2);
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '财务应收', "field": '财务应收', switchable: true, sortable: true, align: 'center', width: 110,
                formatter: function (value, row, index) {
                    if (index == data.length - 1) {
                        return priceOriginalcw.toFixed(2);
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '总费用', "field": '总费用', switchable: true, sortable: true, align: 'center', width: 110,
                formatter: function (value, row, index) {
                    if (index == data.length - 1) {
                        return totalCost.toFixed(2);
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '现金流', "field": '现金流', switchable: true, sortable: true, align: 'center', width: 110,
                formatter: function (value, row, index) {
                    if (index == data.length - 1) {
                        return totalReceipt.toFixed(2);
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '实到金额', "field": '实到金额', switchable: true, sortable: true, align: 'center', width: 110,
                formatter: function (value, row, index) {
                    if (index == data.length - 1) {
                        return totalReceiptEquation.toFixed(2);
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '折扣/活动价', "field": '折扣/活动价', switchable: true, sortable: true, align: 'center', width: 130,
                formatter: function (value, row, index) {
                    if (index == data.length - 1) {
                        return ((parseInt(totalCost) / parseInt(priceOriginal)) * 100).toFixed(2) + "%";
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '消费类型', "field": 'isDo', switchable: true, sortable: true, align: 'center', width: 110,
                formatter: function (value, row, index) {
                    if (value == 3) {
                        return "还欠款"
                    } else if (value == 4) {
                        return "退款"
                    } else if (value == 5) {
                        return "消费"
                    } else if (value == 9) {
                        return "储值变动"
                    } else {
                        return "其他"
                    }
                }

            });
            if (data[0] == null || data[0] == undefined || data[0] == "") {
                console.log('返回数据为空');
            } else {
                for (var i = 0; i < (Object.keys(data[0])).length; i++) {
                    var property = (Object.keys(data[0]))[i];
                    if (property != 'id' && property != 'id2' && property != 'leftDown' && property != 'leftUp' && property != 'rightDown' && property != 'rightUp' && property != '转诊人' && property != '被转诊人' && property != '转诊时间' && property != '转诊备注' && property != '备注' &&
                        property != '结账时间' && property != '初诊时间' && property != '市场建档时间' && property != '时差' && property != '姓名' && property != '性别' && property != '年龄' && property != '病历号' && property != '电话' && property != '牙位' && property != '类别' && property != 'referee'
                        && property != '项目' && property != '总费用' && property != '现金流' && property != '实到金额' && property != '消费类型' && property != '折扣/活动价' && property != '正畸编号' && property != 'ETA编号' && property != '黄页编号' && property != '初诊地'
                        && property != '欠费' && property != '科室' && property != '单据号' && property != 'TC' && property != '推荐人' && property != '医生' &&
                        property != '护士' && property != '一级渠道' && property != '二级渠道' && property != '三级渠道'
                        && property != '一级媒介' && property != '二级媒介' && property != '三级媒介' && property != '编号' && property != '问卷调查' && property != '财务应收'
                        && property != '网电咨询师' && property != '审批数据' && property != '客户状态' && property != 'TC助理' && property != '原价' && property != 'isDo') {
                        arr.push({
                            "title": property,
                            "field": property,
                            switchable: true,
                            sortable: true,
                            align: 'center',
                            width: 110
                        });
                    }
                }
            }
            arr.push({
                "title": '欠费', "field": '欠费', switchable: true, sortable: true, align: 'center', width: 100,
                formatter: function (value, row, index) {
                    if (index == data.length - 1) {
                        return Arrears.toFixed(2);
                    } else {
                        return value;
                    }
                }
            });
            arr.push({"title": '单据号', "field": '单据号', switchable: true, sortable: true, align: 'center', width: 110});
            arr.push({"title": 'TC', "field": 'TC', switchable: true, sortable: true, align: 'center', width: 100});
            arr.push({
                "title": '推荐人',
                "field": 'referee',
                switchable: true,
                sortable: true,
                align: 'center',
                width: 100
            });
            arr.push({"title": 'TC助理', "field": 'TC助理', switchable: true, sortable: true, align: 'center', width: 110});
            arr.push({"title": '医生', "field": '医生', switchable: true, sortable: true, align: 'center', width: 100});
            arr.push({"title": '护士', "field": '护士', switchable: true, sortable: true, align: 'center', width: 100});
            arr.push({"title": '一级渠道', "field": '一级渠道', switchable: true, sortable: true, align: 'center', width: 120});
            arr.push({"title": '二级渠道', "field": '二级渠道', switchable: true, sortable: true, align: 'center', width: 120});
            arr.push({"title": '三级渠道', "field": '三级渠道', switchable: true, sortable: true, align: 'center', width: 120});
            arr.push({"title": '一级媒介', "field": '一级媒介', switchable: true, sortable: true, align: 'center', width: 120});
            arr.push({"title": '二级媒介', "field": '二级媒介', switchable: true, sortable: true, align: 'center', width: 120});
            arr.push({"title": '三级媒介', "field": '三级媒介', switchable: true, sortable: true, align: 'center', width: 120});
            arr.push({
                "title": '网电咨询师', "field": '网电咨询师', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '审批数据', "field": '审批数据', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '正畸编号', "field": '正畸编号', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": 'ETA编号', "field": 'ETA编号', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '黄页编号', "field": '黄页编号', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({"title": '初诊地', "field": '初诊地', switchable: true, sortable: true, align: 'center', width: 120});
            arr.push({"title": '客户状态', "field": '客户状态', switchable: true, sortable: true, align: 'center', width: 120});
            arr.push({
                "title": '转诊人', "field": '转诊人', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '被转诊人', "field": '被转诊人', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({"title": '转诊时间', "field": '转诊时间', switchable: true, sortable: true, align: 'center', width: 180});
            arr.push({
                "title": '转诊备注', "field": '转诊备注', switchable: true, sortable: true, align: 'center', width: 180,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            arr.push({
                "title": '备注', "field": '备注', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>';
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            });
            arr.push({
                "title": '编号', "field": '编号', switchable: true, sortable: true, align: 'center', width: 180,
                formatter: function (value, row, index) {
                    return '<span title="' + value + '">' + value + '</span>';
                },
                cellStyle: function (value, row, index) {
                    return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
                }
            });
            arr.push({
                "title": '问卷调查', "field": '问卷调查', switchable: true, sortable: true, align: 'center', width: 120,
                formatter: function (value, row, index) {
                    if (value == 'null') {
                        return '';
                    } else {
                        return value;
                    }
                }
            });
            $('#CostDetailedTable').bootstrapTable('destroy').bootstrapTable({
                data: data,
                toolbar: '#toolbar',
                cache: false,
                striped: true,
                sidePagination: "client",
                sortOrder: "desc",
                pageSize: 500,
                pageNumber: 1,
                pageList: [500, 1000],
                showToggle: true,
                showColumns: true,
                showExport: true,
                exportDataType: "basic",
                pagination: true,
                strictSearch: true,
                search: true,
                columns: arr,
                height: $(window).height() - 76
            });
            phonePrivileges();
        }
    });
};

function phonePrivileges() {   //隐藏手机号
    var phonePrivileges = parent.$("#phonePrivileges").val();
    console.log(1);
    if (phonePrivileges == 2) {
        $('#CostDetailedTable').bootstrapTable('hideColumn', '电话');
    }
}

$(function () {
    CostDetailedTable.today();
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {
        costDetail()
    });
    checkDate = function () {
        costDetail()
    };
});